<template>
  <div class="home">
    <bread-crumb title="演出风格"></bread-crumb>
    <div class="new_row">
      <el-button type="primary" @click="addShowTag">添加演出风格</el-button>
    </div>
    <el-table :data="datas" border stripe>
      <el-table-column align="center" prop="tag_id" label="风格编号" width="150px"></el-table-column>
      <el-table-column align="center" prop="tag_name" label="风格" width="250px"></el-table-column>
      <el-table-column
            fixed="right"
            label="操作"
            width="250">
            <template slot-scope="scope">
              <el-button @click="editRow(scope.row)" type="primary" size="small">编辑</el-button>
              <el-button @click="delRow(scope.row)" type="danger" size="small">删除</el-button>
            </template>
          </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalRows">
    </el-pagination>
    <el-dialog title="删除提醒" :visible.sync="is_show" @close="is_show=false">
      <span class="text-danger">{{ del_msg }}</span>
      <div slot="footer">
        <el-button @click="is_show=false">取消</el-button>
        <el-button @click="delData" type="danger">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="editOrAddTitle" :visible.sync="edit_is_show" @close="editForm = {}">
      <el-form :model="editForm" class="editForm">
        <el-form-item label="演出风格">
          <el-input v-model="editForm.tag_name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="edit_is_show=false">取消</el-button>
        <el-button @click="saveForm" type="primary">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      datas: [],
      currentPage: 1,
      pageSize: 10,
      totalRows: 0,
      editForm: {},
      editData: {},
      edit_is_show: false,
      disabled: false,
      editOrAddTitle: '修改数据',
      del_msg: '是否删除',
      del_item: {},
      is_show: false
    }
  },
  methods: {
    loadData () {
      this.$http.get('/adminapi/tag/?page=' + this.currentPage + '&page_size=' + this.pageSize).then(resp => {
        const { results, count } = resp.data
        this.datas = results
        this.totalRows = count
      })
    },
    addShowTag () {
      this.editForm.title = '新增演出风格'
      this.edit_is_show = true
      this.disabled = false
    },
    editRow (editData) {
      this.edit_is_show = true
      this.disabled = true
      // 初始显示的数据
      this.editForm = {
        tag_name: editData.tag_name,
        tag_id: editData.tag_id
      }
    },
    saveForm (item) {
      if (this.disabled) {
        this.editForm = item
        // 删除对象的属性(不修改的)
        delete this.editForm.tag_id
        this.$http.patch('/adminapi/tag/' + this.editForm.tag_id + '/', this.editForm).then(resp => {
          this.edit_is_show = false
          this.loadData()
          this.editForm = {} // 清空之前绑定的数据
        })
      } else {
        this.$http.post('/adminapi/tag/', this.editForm).then(resp => {
          console.info(resp.data)
          this.edit_is_show = false
          this.loadData()
        })
      }
    },
    delRow (item) {
      this.del_item = item
      this.is_show = true
      this.del_msg = '亲，真的忍心删除' + item.tag_name + '吗？'
    },
    delData () {
      this.$http.delete('/adminapi/tag/' + this.del_item.tag_id + '/').then(resp => {
        console.info(resp.data)
        this.loadData() // 刷新页面数据，重新装载数据
        this.$message.success('数据删除成功')
        this.is_show = false
      })
    },
    handleSizeChange (size) {
      console.info(size)
      this.currentPage = 1
      this.pageSize = size
      this.loadData()
    },
    handleCurrentChange (page) {
      console.info(page)
      this.currentPage = page
      this.loadData()
    },
    handleClick (tab, event) {
      console.log(tab, event)
    }
  },
  mounted () {
    this.loadData()
  }
}
</script>

<style>
</style>
